<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态创建元素示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fa;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            text-align: center;
            width: 400px;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        .element-box {
            margin-top: 20px;
            padding: 20px;
            background-color: #ffeb3b;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-top: 20px;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>动态创建元素示例</h1>
        <button id="createButton">点击创建元素</button>
        <div id="elementContainer"></div>
    </div>

    <script>
        // 基于给定的字符串创建DOM元素
        const createElement = str => {
            const element = document.createElement('div');
            element.innerHTML = str;
            return element.firstElementChild;
        };

        // 获取按钮和元素容器
        const createButton = document.getElementById('createButton');
        const elementContainer = document.getElementById('elementContainer');

        // 点击按钮后创建并添加元素
        createButton.addEventListener('click', () => {
            const newElement = createElement('<div class="element-box">这是动态创建的元素！</div>');
            elementContainer.appendChild(newElement);
        });
    </script>

</body>

</html>